package store.controller;

import store.dao.RestockDAO;
import store.dao.SaleDAO;
import store.po.Restock;
import store.po.Sale;
import store.po.SaleItem;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/sale/add")
public class AddSale extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            String[] goodsIds=request.getParameterValues("goodsId");
            String[] counts=request.getParameterValues("count");
            String now=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            Sale s=new Sale();
            s.setTradeTime(now);
            for (int i=0;i<goodsIds.length;i++){
                SaleItem saleItem=new SaleItem();
                saleItem.setGoodsId(Integer.parseInt(goodsIds[i]));
                saleItem.setCount(Integer.parseInt(counts[i]));
                s.getItems().add(saleItem);
            }
            new SaleDAO().save(s);
            request.getRequestDispatcher("/sale/list").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
